package org.admin.controller;

import core.Data;
import core.data.DataResult;
import org.admin.entity.dto.PermsGroupDto;
import org.admin.service.PermsGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/perms/group")
public class PermsGroupController {
    @Autowired
    private PermsGroupService service;
    @GetMapping("/query")
    public DataResult query(@RequestParam Map<String, Object> get){
        return Data.create(get).<PermsGroupDto>addHandle("params", data -> {
                    new PermsGroupDto().validate(new String[] {
                        "name", "size", "*current"
                    }).check(data);
                }).addHandle("query", service::query)
                .start().getResult();
    }

    @GetMapping("/fetch")
    public DataResult fetch(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<PermsGroupDto>addHandle("params", data -> {
                    new PermsGroupDto().validate(new String[] {
                            "*id",
                    }).check(data);
                }).addHandle("query", service::fetch)
                .start().getResult();
    }

    @PostMapping("/create")
    public DataResult create (@RequestBody Map<String, Object> post){
        // id问题没有解决
        return Data.create(post)
                .<PermsGroupDto>addHandle("params", data -> {
                    new PermsGroupDto().validate(new String[] {
                            "id", "*name", "detail"
                    }).check(data);
                }).addHandle("query", service::create)
                .start().getResult();
    }

    @GetMapping("/info")
    public DataResult info(@RequestParam Map<String, Object> get){
        return Data.create(get)
                .<PermsGroupDto>addHandle("params", data -> {
                    new PermsGroupDto().validate(new String[] {
                            "id",
                    }).check(data);
                }).addHandle("query", service::fetch)
                .start().getResult();

    }

    @PostMapping("/remove")
    public DataResult remove(@RequestBody Map<String, Object> post){
        return Data.create(post)
                .<PermsGroupDto>addHandle("params", data -> {
                    new PermsGroupDto().validate(new String[] {
                            "id", "ids"
                    }).check(data);
                }).addHandle("query", service::remove)
                .start().getResult();
    }
}
